<?php

namespace app\admin\controller;


use app\admin\model\Wuliu;
use cmf\controller\AdminBaseController;
use think\facade\Db;

            /**
              * Class WuliuController
              * @package app\admin\controller
              * @adminMenuRoot(
              *     'name'   =>'物流信息管理',
              *     'action' =>'index',
              *     'parent' =>'',
              *     'display'=> true,
              *     'order'  => 10000,
              *     'icon'   =>'',
              *     'remark' =>''
              * )
            */
class WuliuController extends AdminBaseController
{
    
    public function index(){

        $user_arr = Db::name('user')
            ->column('user_nickname', 'id');
        $this->assign('user_arr', $user_arr);

        $wuliutype_arr = array(1 => '销售', 2 => '采购', 3 => '其他');
        $this->assign('wuliutype_arr', $wuliutype_arr);
        $is_end_arr = array(1 => '未完结', 2 => '已完结');
        $this->assign('is_end_arr', $is_end_arr);
        $is_settle_arr = array(1 => '未结算', 2 => '已结算');
        $this->assign('is_settle_arr', $is_settle_arr);
        $is_chartered_car = array(1 => '包车', 2 => '拼车');
        $this->assign('is_chartered_car', $is_chartered_car);
        $c_type_arr = array(1 => '内贸', 2 => '调试', 3=>'委外加工', 4=>'外贸');
        $this->assign('c_type_arr', $c_type_arr);
        //搜索部分
//        $key_status = input('get.key_status/d');

        $lists = Wuliu::order('id desc');
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }
        $lists = $lists->where('delete_time', 0);
//        var_dump(111);
        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);

        return $this->fetch('', ['lists' => $lists->toArray(), 'page' => $lists->render(),'domain'=>request()->domain().'/upload/']);
    }

    public function add(){
        $c_type_arr = array(
            0 => array('id' => 1, 'name' => '内贸'),
            1 => array('id' => 2, 'name' => '调试'),
            2 => array('id' => 3, 'name' => '委外加工'),
            3 => array('id' => 4, 'name' => '外贸'),
        );
        $this->assign('c_type_arr', $c_type_arr);
        return $this->fetch('',['domain'=>request()->domain().'/upload/']);
    }

    public function addPost(){
        $params = array_map(function($v){
            return is_array($v)?implode(',',$v):$v;
        },input('post.'));

        if(!$params){
            $this->error('非法参数');
        }
        $params['newdate'] = strtotime($params['newdate']);
        $params['update_time'] = time();
        $params['add_time'] = time();
        $params['admin_id'] = cmf_get_current_admin_id();
        Wuliu::create($params);

        $this->success('操作成功','index');
    }

    public function edit(){
        $id = input('get.id/d');

        if(!is_numeric($id)){
            $this->error('非法参数');
        }
        $c_type_arr = array(
            0 => array('id' => 1, 'name' => '内贸'),
            1 => array('id' => 2, 'name' => '调试'),
            2 => array('id' => 3, 'name' => '委外加工'),
            3 => array('id' => 4, 'name' => '外贸'),
        );
        $this->assign('c_type_arr', $c_type_arr);
        $res = Wuliu::where('id',$id)->find();
        

        return $this->fetch('',['vo'=>$res,'domain'=>request()->domain().'/upload/']);
    }

    public function editPost(){
        $params = array_map(function($v){
                    return is_array($v)?implode(',',$v):$v;
                    },input('post.'));

        if(!$params || empty($params['id'])){
            $this->error('非法参数');
        }
        $params['newdate'] = strtotime($params['newdate']);
        $params['update_time'] = time();
        $params['admin_id'] = cmf_get_current_admin_id();
        Wuliu::update($params);

        $this->success('操作成功','index');
    }

    public function del(){
        $id = input('get.id/d');

        if(!(is_numeric($id) && $id>0)){
            $this->error('非法参数');
        }

        Wuliu::where('id',$id)->update(array('delete_time' => time(), 'admin_id' => cmf_get_current_admin_id()));

        $this->success('删除成功');
    }

    public function field(){
         $id = input('get.id/d');
         $field = input('get.field');

            if(!(is_numeric($id) && $id>0 && $field)){
            $this->error('非法参数');
        }

        $info = Wuliu::field($field)->where('id',$id)->find();

        $this->success('ok','',$info[$field]);
    }

    /**excel 导出
     *      *   @adminMenu(
     *     'name'   => 'excel导出',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '接收计划',
     *     'param'  => ''
     * )
     * @return void
     */
    public function outexcel()
    {
        //要导出的数据,二维
        $lists = Wuliu::order('id desc');
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }
        $lists = $lists->where('delete_time', 0);
        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);
        $data = $lists->toArray();
        $data = $data['data'];
        $newdata = array();

        $user_arr = Db::name('user')
            ->column('user_nickname', 'id');


        $wuliutype_arr = array(1 => '销售', 2 => '采购', 3 => '其他');

        $is_end_arr = array(1 => '未完结', 2 => '已完结');

        $is_settle_arr = array(1 => '未结算', 2 => '已结算');

        $is_chartered_car = array(1 => '包车', 2 => '拼车');

        foreach ($data as $k => $vo) {
            $newdata[$k] = $vo;
            $newdata[$k]['add_time'] = date('Y-m-d H:i', $vo['add_time']);
            $newdata[$k]['newdate'] = date('Y-m-d H:i', $vo['newdate']);
            $newdata[$k]['update_time'] = date('Y-m-d H:i', $vo['update_time']);
            $newdata[$k]['admin_id'] = isset($user_arr[$vo['admin_id']]) ? $user_arr[$vo['admin_id']] : '';
            $newdata[$k]['wuliutype'] = isset($wuliutype_arr[$vo['wuliutype']]) ? $wuliutype_arr[$vo['wuliutype']] : '';
            $newdata[$k]['chartered_car'] = isset($is_chartered_car[$vo['chartered_car']]) ? $is_chartered_car[$vo['chartered_car']] : '';
            $newdata[$k]['is_end'] = isset($is_end_arr[$vo['is_end']]) ? $is_end_arr[$vo['is_end']] : '';
            $newdata[$k]['is_settle'] = isset($is_settle_arr[$vo['is_settle']]) ? $is_settle_arr[$vo['is_settle']] : '';


        }

        $head = array( '类别',
            '日期',
            '起始地',
            '目的地',
            '货物名称',
            '平台类型',
            '车辆类型',
            '包车类型',
            '合同号',
            '规格',
            '重量(吨)',
            '金额',
            '是否完结',
            '是否结算',
            '备注',
            '添加时间',
            '更新时间',
            '最后操作人',);

        //数据中对应的字段，用于读取相应数据：
        $keys = [
            'wuliutype',
            'newdate',
            'start_address',
            'end_address',
            'name',
            'platform_type',
            'car_type',
            'chartered_car',
            'ht_number',
            'specs',
            'weight',
            'money',
            'is_end',
            'is_settle',
            'remrk',
            'add_time',
            'update_time',
            'admin_id',
        ];


//        $head = array('id', 'name');//设置表头
//        $keys = ['id','name'];//数据中表头对应的字段，用于读取相应数据
//        var_dump($data);
        excel_out('物流导出' . date('ymdhis', time()), $newdata, $head, $keys);

    }

}